package uk.petesoft.remindme;

import javax.swing.SpinnerNumberModel;

public class SpinnerCircularNumberModel extends SpinnerNumberModel{
	
	private static final long serialVersionUID = -3693770775833804563L;

	public SpinnerCircularNumberModel(int value, int minimum, int maximum, int stepSize) {
		super(value, minimum, maximum, stepSize);
	}

	/**
	 * Overrides the getNextValue method to return the first object
	 * in the model if it has reached the end of the list
	 */
	@Override
	public Object getNextValue() {
		int maximum = (Integer)this.getMaximum();
		int minimum = (Integer)this.getMinimum();
		int currentValue = (Integer)this.getValue();
		
		if (currentValue == maximum) {
			return minimum;
		} else {
			return currentValue + (Integer)this.getStepSize();
		}
	}
	
	/**
	 * Overrides the getPreviousValue method to return the last object
	 * in the model if it has reached the beginning of the list
	 */	
	@Override
	public Object getPreviousValue() {
		int maximum = (Integer)this.getMaximum();
		int minimum = (Integer)this.getMinimum();
		int currentValue = (Integer)this.getValue();
		
		if (currentValue == minimum) {
			return maximum;
		} else {
			return currentValue - (Integer)this.getStepSize();
		}
	}
}
